package org.vectrics.recruiter.job;

import java.util.HashSet;
import java.util.Iterator;

import org.apache.log4j.Category;
   
public class SearchUtil {
    private static Category log = Category.getInstance(SearchUtil.class);
    private static HashSet smallWordList = null;
    
    private static HashSet getSmallWordList() {
        if (smallWordList == null) {
            smallWordList = new HashSet();
            smallWordList.add("and");
            smallWordList.add("the");
            smallWordList.add("an");
            smallWordList.add("to");
            smallWordList.add("from");
            smallWordList.add("there");
        }
        return(smallWordList);
    }
    
    public static boolean isWordNumber(String word) {
        boolean result = false;
        int i = 0; 
         while ((result == true) && (i < word.length())) {
            char c = word.charAt(i);
            if (!((c == '0') ||
                    (c == '1') ||
                    (c == '2') ||
                    (c == '3') ||
                    (c == '4') ||
                    (c == '5') ||
                    (c == '6') ||
                    (c == '7') ||
                    (c == '8') ||
                    (c == '9') ||
                    (c == '.') )) {
                  result = false;
              }
              i++;
        }
        return(result);
    }

    public static boolean isWordSmall(String wordParam) {
        String word = wordParam.trim().toLowerCase();
        boolean result = false;
        if (word.length() <= 1) {
            result = true;
        }
        
        if (result == false) {
            result = isWordNumber(word);
            if (result == false) {
                result = getSmallWordList().contains(word);
            }
        }
        return(result);
    }
    
    
    public static HashSet parseSearchWords(String str) {
        log.debug("parseSearchWords() called. str = " + str);
        HashSet searchWords = new HashSet();

        if (str == null) {
            log.error("parseSearchWords() - passed a null string");
        }
        else {
            int i = 0;
            int startPrevWord = 0;
            while (i < str.length()) {
                
                log.debug("parseSearchWords() - finding end of current word.");
                while ((i < str.length() && (str.charAt(i) != ' '))) {
                    i++;
                }
                
                if (i > startPrevWord) {
                    String word = str.substring(startPrevWord, i);
                    log.debug("parseSearchWords() - found word: " + word);
                    searchWords.add(word.toLowerCase());
    
                    log.debug("parseSearchWords() - finding start of next word.");
                    while ((i < str.length()) && (str.charAt(i) == ' ')) {
                        i++;
                    }
                    startPrevWord = i;
                }
            }
        }
        return(searchWords);
    }
    
    
	public static void addImpliedSearchWords(HashSet searchWordList) {
		HashSet addWords = new HashSet();
		Iterator iter = searchWordList.iterator();
		while (iter.hasNext()) {
			String word = (String)iter.next();
            
			if (word.equals("j2ee")) {
				addWords.add("ejb");
				addWords.add("enterprise");
			}
			else if (word.equals("ejb")) {
				addWords.add("j2ee");
			}
			else if (word.equals("consult")) {
				addWords.add("consultant");
				addWords.add("consulting");
				addWords.add("consults");
			}
			else if (word.equals("consults")) {
				addWords.add("consultant");
				addWords.add("consulting");
			}
			else if (word.equals("consultant")) {
				addWords.add("consulting");
				addWords.add("consults");
			}
			else if (word.equals("java")) {
				addWords.add("j2ee");
				addWords.add("jms");
				addWords.add("ejb");
			}
			else if ((word.equals("programming")) || (word.equals("developer")) || (word.equals("programmer"))) {
				addWords.add("programmer");
				addWords.add("programming");
				addWords.add("developer");
			}
            
			else if (word.equals(".net")) {
				addWords.add("microsoft");
				addWords.add("c#");
				addWords.add("visual");
				addWords.add("basic");
			}
			else if (word.equals("jms")) {
				addWords.add("java");
			}
			else if ((word.equals("vp")) || (word.equals("v.p."))) {
				addWords.add("vice");
				addWords.add("executive");
				addWords.add("president");
			}
            
		}
		searchWordList.addAll(addWords);                
	}

    
}
